package Web.Servlet;

import Domain.Cart;
import Domain.Collection;
import Domain.Product;
import Domain.User;
import Service.CartService;
import Service.CollectionService;
import Service.Impl.CartServiceImpl;
import Service.Impl.CollectionServiceImpl;
import Service.Impl.ProductServiceImpl;
import Service.Impl.UserServiceImpl;
import Service.ProductService;
import Service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/CollectionServlet")
public class CollectionServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");


        if(action.equals("IntoCollection")){
            String id = request.getParameter("id");
            CollectionService service = new CollectionServiceImpl();
            List<Collection> collections = service.FindCollectionByUserid(Integer.parseInt(id));
            UserService service1 = new UserServiceImpl();
            List<User> list = service1.findUserById(Integer.parseInt(id));
            request.setAttribute("user",list.get(0));
            request.setAttribute("product",collections);

            request.getRequestDispatcher("Collection/CollectionPage.jsp").forward(request,response);
        }

        if(action.equals("delCollection")){
            String id = request.getParameter("id");
            CollectionService service = new CollectionServiceImpl();
            List<Collection> list = service.FindCollectionById(Integer.parseInt(id));
            Collection collection = list.get(0);

            service.DelCollectionById(Integer.parseInt(id));//先删除要删除得哪个收藏

            response.sendRedirect(request.getContextPath()+"/CollectionServlet?action=IntoCollection&id="+collection.getCid());
        }

        if(action.equals("AddCollection")){
            String uid = request.getParameter("uid");
            String pid = request.getParameter("pid");

            CollectionService service = new CollectionServiceImpl();
            service.AddCollection(Integer.parseInt(uid),Integer.parseInt(pid));

            ProductService service1 = new ProductServiceImpl();
            List<Product> list = service1.FindProductById(Integer.parseInt(pid));
            Product product = list.get(0);

            if(!uid.equals("")){
                UserService service2 = new UserServiceImpl();
                List<User> list1 = service2.findUserById(Integer.parseInt(uid));
                User user = list1.get(0);
                request.setAttribute("user",user);
            }

            request.setAttribute("product",product);
            request.getRequestDispatcher("Product/Details.jsp").forward(request,response);
        }

        if(action.equals("AddCollection1")){
            String uid = request.getParameter("uid");
            String pid = request.getParameter("pid");

            CollectionService service = new CollectionServiceImpl();
            service.AddCollection(Integer.parseInt(uid),Integer.parseInt(pid));

            CartService service1 = new CartServiceImpl();
            List<Cart> carts = service1.FindCartByUserid(Integer.parseInt(uid));

            if(!uid.equals("")){
                UserService service2 = new UserServiceImpl();
                List<User> list1 = service2.findUserById(Integer.parseInt(uid));
                User user = list1.get(0);
                request.setAttribute("user",user);
            }

            request.setAttribute("product",carts);
            request.getRequestDispatcher("Cart/CartPage.jsp").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
